/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.dao.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sigmatrix.h5.dao.DisRebateAuditDao;
import com.sigmatrix.h5.dao.DisRebateDetailDao;
import com.sigmatrix.h5.entity.DisRebateDetail;
import com.sigmatrix.h5.utils.CommonUtils;
import com.sigmatrix.h5.utils.SimpleRowMapper;
import com.sigmatrix.h5.web.mode.AccumulatedRebate;
import com.sigmatrix.h5.web.mode.RebateList.Detail;
import com.sigmatrix.sc.jdbc.common.dao.DaoImpl;
import com.sigmatrix.sc.jdbc.common.lang.Page;

/**
 *@ClassName: DisRebateDetailDaoImpl
 *@Description: 
 *@author MH
 *@date 2017年8月11日 下午4:59:34
 */
@Repository
public class DisRebateDetailDaoImpl extends DaoImpl<DisRebateDetail,Integer> implements DisRebateDetailDao {
	@Autowired
	private DisRebateAuditDao disRebateAuditDao;
	@Override
	public BigDecimal findTotalRebateByDealerId(int dealerId) {
		final StringBuffer sql = new StringBuffer();
		sql.append("select sum(_rebate_amount) from ").append(tableName());
		sql.append(" where _dealer_id = ?");
		logger.info("sql="+sql.toString());
		logger.info("param="+dealerId);
		return jdbcTemplate.queryForObject(sql.toString(), BigDecimal.class, dealerId);
	}

	@Override
	public AccumulatedRebate statistic(Integer dealerId,Short dealerType) {
		final StringBuffer sql = new StringBuffer();
		sql.append("select sum(_rebate_amount) _accumulated_rebate,0 _accumulated_with_drawals from ").append(tableName());
		sql.append(" where _rebate_amount > 0 and  _dealer_id = ").append(dealerId);
		sql.append(" and _dealer_type = ").append(dealerType);
		sql.append(" union select 0 _accumulated_rebate,sum(_rebate_amount) _accumulated_with_drawals from ").append(tableName());
		sql.append(" where _rebate_amount <0 and _dealer_id = ").append(dealerId);
		sql.append(" and _dealer_type = ").append(dealerType);
		logger.info("sql="+sql.toString());
		logger.info("param="+dealerId);
		List<AccumulatedRebate> list = jdbcTemplate.query(sql.toString(), SimpleRowMapper.createDefault(AccumulatedRebate.class));
		if(CommonUtils.isNotEmpty(list)){
			AccumulatedRebate r = new AccumulatedRebate();
			for (AccumulatedRebate _r : list) {
				if(_r.getAccumulatedRebate().compareTo(new BigDecimal(0)) > 0 ){
					r.setAccumulatedRebate(_r.getAccumulatedRebate());
				}
				if(_r.getAccumulatedWithDrawals().compareTo(new BigDecimal(0)) < 0 ){
					r.setAccumulatedWithDrawals(_r.getAccumulatedWithDrawals());
				}
			}
			return r;
		}
		return null;
	}

	@Override
	public Page<Detail> rebateList(int dealerId, Short dealerType, int pageNo, int pageSize) {
		StringBuffer sql = new StringBuffer();
		sql.append("select b._create_time,b._dealer_id,b._dealer_type,b._amount,b._source,b._rebate_activity_id from(");
		sql.append("select a._create_time,a._dealer_id,a._dealer_type,a._amount,a._source,a._rebate_activity_id from ");
		sql.append(disRebateAuditDao.tableName()).append(" a ");
		sql.append("where 1=1 and a._dealer_id = ").append(dealerId);
		sql.append(" and a._dealer_type = ").append(dealerType);
		sql.append(" UNION ALL ");
		sql.append("select 	a._rebate_time,a._dealer_id,a._dealer_type,a._rebate_amount,null,null ");
		sql.append(" from ").append(tableName()).append(" a ");
		sql.append("where a._rebate_amount < 0 and a._dealer_id = ").append(dealerId);
		sql.append(" and a._dealer_type = ").append(dealerType);
		sql.append(") b");
		Map<String,String> order = new HashMap<String, String>();
		order.put("_create_time", "desc");
		return query(sql.toString(), order, pageNo, pageSize, SimpleRowMapper.createDefault(Detail.class), new Object[0]);
	}
	
	
	public static void main(String[] args) {
		final StringBuffer sql = new StringBuffer();
		sql.append("select sum(_rebate_amount) _accumulated_rebate,0 _accumulated_with_drawals from ").append("DisRebateDetailtable");
		sql.append(" where _rebate_amount > 0 and  _dealer_id = ").append("dealerId");
		sql.append(" and _dealer_type = ").append("dealerType");
		sql.append(" union select 0 _accumulated_rebate,sum(_rebate_amount) _accumulated_with_drawals from ").append("DisRebateDetailtable");
		sql.append(" where _rebate_amount <0 and _dealer_id = ").append("dealerId");
		sql.append(" and _dealer_type = ").append("dealerType");
		
		System.err.println(sql);
	}

}
